# utils/file_utils.py - 文件工具
import os
from pathlib import Path

def format_file_size(size_bytes):
    """格式化文件大小显示"""
    if not size_bytes:
        return "未知大小"
    
    try:
        size_bytes = int(size_bytes)
        if size_bytes == 0:
            return "0 B"
        
        units = ['B', 'KB', 'MB', 'GB']
        size = float(size_bytes)
        unit_index = 0
        
        while size >= 1024 and unit_index < len(units) - 1:
            size /= 1024
            unit_index += 1
        
        return f"{size:.1f} {units[unit_index]}"
    except (ValueError, TypeError):
        return "未知大小"

def generate_version_filename(base_name: str, version: str, original_filename: str) -> str:
    """生成版本文件名"""
    original_ext = Path(original_filename).suffix
    return f"{base_name}V{version}{original_ext}"

def ensure_directory_exists(path: Path):
    """确保目录存在"""
    path.mkdir(parents=True, exist_ok=True)

def safe_delete_file(file_path: Path):
    """安全删除文件"""
    try:
        if file_path.exists():
            file_path.unlink()
        return True
    except Exception:
        return False